package com.shensi.adminCollect.controller.station;

import com.alibaba.fastjson.JSONObject;
import com.shensi.adminCollect.common.constant.HttpStatus;
import com.shensi.adminCollect.common.constant.UserConstants;
import com.shensi.common.core.controller.BaseController;
import com.shensi.common.core.domain.AjaxResult;
import com.shensi.common.core.page.PageDomain;
import com.shensi.common.core.page.TableDataInfo;
import com.shensi.common.core.page.TableSupport;
import com.shensi.adminCollect.common.utils.StringUtils;
import com.shensi.adminCollect.common.utils.poi.ExcelUtil;
import com.shensi.adminCollect.domain.station.Supplier;
import com.shensi.adminCollect.service.station.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


import com.shensi.common.core.domain.model.LoginUser;
import com.shensi.common.core.utils.SecurityUtils;
import com.shensi.common.core.domain.entity.SysUser;

@RestController
@RequestMapping("/supplier")
@Slf4j
@Api(value = "供应商管理接口")
public class SupplierController extends BaseController {

    @Autowired
    private SupplierService supplierService;

    @PostMapping("/add")
    @ApiOperation(value = "新增供应商",httpMethod = "POST")
    public AjaxResult addSupplier(@RequestBody Supplier supplier){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(Objects.isNull(user)){
            return AjaxResult.error("该账号不存在，请求登录");
        }
        if(UserConstants.LEVEL_ONE.equals(user.getUserLevel()) || Objects.isNull(user.getUserLevel())){
            if(Objects.isNull(supplier) || StringUtils.isEmpty(supplier.getSupplierName())){
                return AjaxResult.error("请求参数异常");
            }

            if(supplier.getSupplierName().length() > 50){
                return AjaxResult.error("供应商名称超出长度限制");
            }
            if(StringUtils.isNotEmpty(supplier.getContacts()) && supplier.getContacts().length() > 50){
                return AjaxResult.error("联系人超出长度限制");
            }
//            if(StringUtils.isNotEmpty(supplier.getPhone()) && !RegexUtils.isPhoneNum(supplier.getPhone())){
//                return AjaxResult.error("请输入正确的手机号");
//            }
            if(StringUtils.isNotEmpty(supplier.getPhone()) && supplier.getPhone().length() > 50){
                return AjaxResult.error("请输入正确的手机号");
            }
            if(StringUtils.isNotEmpty(supplier.getAddress()) && supplier.getAddress().length() > 50){
                return AjaxResult.error("地址超出长度限制");
            }
            if(StringUtils.isNotEmpty(supplier.getNotes()) && supplier.getNotes().length() > 200){
                return AjaxResult.error("备注超出长度限制");
            }
            supplier.setCreateUser(user.getUserId());
            log.info("addSupplier 请求参数：[{}]", JSONObject.toJSONString(supplier));
            int i = supplierService.saveSupplier(supplier);
            log.info("addSupplier 返回结果：[{}]", i);
            if(i <= 0){
                return AjaxResult.error("新增供应商失败");
            }
            return AjaxResult.success();
        } else {
            return AjaxResult.error("该账号不能新增供应商");
        }
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询供应商列表",httpMethod = "POST")
    public TableDataInfo selectSupplierList(@RequestBody Supplier supplier){
        if(Objects.isNull(supplier) || Objects.isNull(supplier.getPageNum())
                || Objects.isNull(supplier.getPageSize())){
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.ERROR);
            rspData.setMsg("请求参数异常");
            rspData.setRows(null);
            rspData.setTotal(0);
            return rspData;
        }

        Supplier newSupplier = supplierService.getSupplierIdList(supplier);
        log.info("selectSupplierList 请求参数：[{}]", JSONObject.toJSONString(supplier));
        PageDomain pageDomain = TableSupport.buildPageRequest();
        pageDomain.setPageNum(supplier.getPageNum());
        pageDomain.setPageSize(supplier.getPageSize());
        startPage(pageDomain);
        List<Supplier> supplierList = supplierService.selectSupplierList(newSupplier);
        log.info("selectSupplierList 响应结果：[{}]", supplierList.size());
        return getDataTable(supplierList);
    }

    @GetMapping("/allList")
    @ApiOperation(value = "查询所有供应商",httpMethod = "GET")
    public AjaxResult selectSupplierAllList(){
        return AjaxResult.success(supplierService.selectSupplierAllList());
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改供应商信息",httpMethod = "POST")
    public AjaxResult updateSupplier(@RequestBody Supplier supplier){
        if(Objects.isNull(supplier) || Objects.isNull(supplier.getSupplierId())){
            return AjaxResult.error("请求参数异常");
        }
        if(supplier.getSupplierName().length() > 50){
            return AjaxResult.error("供应商名称超出长度限制");
        }
        if(StringUtils.isNotEmpty(supplier.getContacts()) && supplier.getContacts().length() > 50){
            return AjaxResult.error("联系人超出长度限制");
        }
//            if(StringUtils.isNotEmpty(supplier.getPhone()) && !RegexUtils.isPhoneNum(supplier.getPhone())){
//                return AjaxResult.error("请输入正确的手机号");
//            }
        if(StringUtils.isNotEmpty(supplier.getPhone()) && supplier.getPhone().length() > 50){
            return AjaxResult.error("请输入正确的手机号");
        }
        if(StringUtils.isNotEmpty(supplier.getAddress()) && supplier.getAddress().length() > 50){
            return AjaxResult.error("地址超出长度限制");
        }
        if(StringUtils.isNotEmpty(supplier.getNotes()) && supplier.getNotes().length() > 200){
            return AjaxResult.error("备注超出长度限制");
        }
        log.info("updateSupplier 请求参数：[{}]", JSONObject.toJSONString(supplier));
        int i = supplierService.updateSupplier(supplier);
        log.info("updateSupplier 返回结果：[{}]", i);
        if(i <=0 ){
            return AjaxResult.error("修改供应商信息失败");
        }
        return AjaxResult.success();
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除供应商",httpMethod = "GET")
    public AjaxResult deleteSupplier(@RequestParam("supplierIds") String supplierIds){
        if(StringUtils.isEmpty(supplierIds)){
            return AjaxResult.error("请求参数异常");
        }
        String[] split = supplierIds.split(",");
        List<Long> supplierIdList = new ArrayList<>();
        for (String supplierId : split) {
            supplierIdList.add(Long.valueOf(supplierId));
        }
        log.info("deleteSupplier 请求参数：[{}]", supplierIds);
        int i = supplierService.deleteSupplier(supplierIdList);
        log.info("deleteSupplier 返回结果：[{}]", i);
        if(i <= 0){
            return AjaxResult.error("删除供应商失败");
        }
        return AjaxResult.success();
    }

    /**
     * 供应商列表 导出excel
     * */
    @PostMapping("/export")
    @ApiOperation(value = "导出供应商列表",httpMethod = "POST")
    public AjaxResult supplierExport(@RequestBody Supplier supplier){
        Supplier newSupplier = supplierService.getSupplierIdList(supplier);
        newSupplier.setPageSize(null);
        newSupplier.setPageNum(null);
        log.info("supplierExport 请求参数：[{}]", JSONObject.toJSONString(newSupplier));
        List<Supplier> supplierList = supplierService.selectSupplierList(newSupplier);
        log.info("supplierExport 响应结果：[{}]", supplierList.size());
        ExcelUtil<Supplier> supplierExcelUtil = new ExcelUtil<>(Supplier.class);
        return supplierExcelUtil.exportExcel(supplierList,"供应商");
    }

}
